select dbo.OuterBorder(dbo.UnionAggregate(graph)) from Graphs
where id in (4, 5, 6)

select dbo.TopBorder(dbo.UnionAggregate(graph)) from Graphs
where id in (4, 5, 6)

select dbo.BottomBorder(dbo.UnionAggregate(graph)) from Graphs
where id in (4, 5, 6)

select dbo.UnionAggregate(graph) from Graphs
where id in (4, 5, 6)

select dbo.UnionAggregate(graph) from Graphs
where id between 6 and 9

select dbo.EnvelopeAgg(graph) from Graphs
where id between 6 and 9

SELECT dbo.AdjacencyPolygon(graph, 0.2) from Graphs
where id =6

select * from Graphs where id in (6, 7, 8, 9)

truncate table Graphs


DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 0);
SELECT @g.STNumPoints();

DECLARE @g geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 2 2, 0 2, 2 0)', 0);
SELECT @g.STBoundary()

DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('LINESTRING(0 0, 1 0)', 0);
SET @h = geometry::STGeomFromText('LINESTRING(0 1, 1 1)', 0);
set @g = @g.STUnion(@h)
select @g.STConvexHull()
--select @h;
SELECT @g.STWithin(@h);



-- Unite top, middle and bottom lines of union of several graphs
DECLARE @source geometry, @average geometry, @top geometry, @bottom geometry;
select @source = dbo.UnionAggregate(graph) from Graphs
where id in (4, 5, 6);
set @average = dbo.AverageGraph(@source);
select @top = dbo.TopBorder(@source);
select @bottom = dbo.BottomBorder(@source);
select @average.STUnion(@top).STUnion(@bottom);



DECLARE @g geometry;
DECLARE @h geometry;
select @g = dbo.AdjacencyPolygon(graph, 0.2) from Graphs where id =6
select @h = graph from Graphs where id =6
SELECT @g
SELECT @h
SELECT @g.STWithin(@h);
SELECT @h.STWithin(@g);



DECLARE @g geometry;
DECLARE @h geometry;
SET @g = geometry::STGeomFromText('POLYGON((-2 -2, 2 -2, 2 2, -2 2, -2 -2))', 0);
select @h = dbo.AdjacencyPolygon(graph, 0.2) from Graphs where id =6
SELECT @g.STWithin(@h);
SELECT @h.STWithin(@g);
SELECT @g.STContains(@h);
SELECT @h.STContains(@g);


 select geometry::STGeomFromText('LINESTRING(0 0, 1 0)', 0);
 
 select dbo.AdjacencyPolygon(geometry::STGeomFromText('LINESTRING(0 0, 1 0)', 0), 1);
 
 
 --{POINT (0.99916669726371765 0)}
 --{LINESTRING (0.999167 -1, 0.999167 1)}
 --{LINESTRING (0.99916669726371765 0, 0.99000009894371033 -0.0625, 0.98083341121673584 -0.125, 0.96937510371208191 -0.1875, 0.960208386182785 -0.25, 0.94875010848045349 -0.3125, 0.93958339095115662 -0.36875000596046448, 0.93041670322418213 -0.43125000596046448, 0.9189583957195282 -0.48750001192092896, 0.90979170799255371 -0.543749988079071, 0.89833340048789978 -0.59375, 0.88916671276092529 -0.643750011920929, 0.87770840525627136 -0.6875, 0.86854168772697449 -0.737500011920929, 0.85937508940696716 -0.77500000596046448, 0.84791669249534607 -0.8125, 0.83875009417533875 -0.84999999403953552, 0.82729169726371765 -0.88124999403953552, 0.81812509894371033 -0.91249999403953552, 0.80895841121673584 -0.93125000596046448, 0.79750010371208191 -0.956250011920929, 0.788333386182785 -0.96875, 0.77687498927116394 -0.987500011920929, 0.76770839095115662 -0.99375000596046448, 0.75854170322418213 -1, 0.7470833957195282 -1, 0.73791670799255371 -0.99375000596046448, 0.72645840048789978 -0.987500011920929, 0.71729171276092529 -0.981249988079071, 0.708125114440918 -0.96250000596046448, 0.69666668772697449 -0.94374999403953552, 0.68750008940696716 -0.925000011920929, 0.67604169249534607 -0.893750011920929, 0.66687509417533875 -0.86875000596046448, 0.65770828723907471 -0.831250011920929, 0.64625009894371033 -0.793749988079071, 0.63708341121673584 -0.75624999403953552, 0.62562501430511475 -0.71250000596046448, 0.616458386182785 -0.668749988079071, 0.60499998927116394 -0.61875000596046448, 0.59583339095115662 -0.56874999403953552, 0.58666670322418213 -0.512499988079071, 0.5752083957195282 -0.45625001192092896, 0.56604170799255371 -0.40000000596046448, 0.55458340048789978 -0.34375, 0.54541671276092529 -0.28125, 0.536250114440918 -0.21875, 0.52479168772697449 -0.15625, 0.51562508940696716 -0.09375, 0.50416669249534607 -0.03125, 0.49500000476837158 0.03125, 0.48583340644836426 0.09375, 0.47437500953674316 0.15625, 0.46520841121673584 0.21875, 0.45375001430511475 0.28125, 0.44458338618278503 0.34375, 0.43541669845581055 0.40000000596046448, 0.42395839095115662 0.45625001192092896, 0.41479170322418213 0.512499988079071, 0.4033333957195282 0.56874999403953552, 0.39416670799255371 0.61875000596046448, 0.38270840048789978 0.668749988079071, 0.37354171276092529 0.71250000596046448, 0.36437499523162842 0.75624999403953552, 0.35291668772697449 0.793749988079071, 0.34375 0.831250011920929, 0.33229169249534607 0.86875000596046448, 0.32312500476837158 0.893750011920929, 0.31395828723907471 0.925000011920929, 0.30250000953674316 0.94374999403953552, 0.29333341121673584 0.96250000596046448, 0.28187501430511475 0.981249988079071, 0.27270838618278503 0.987500011920929, 0.26354169845581055 0.99375000596046448, 0.25208339095115662 1, 0.24291670322418213 1, 0.2314583957195282 0.99375000596046448, 0.22229170799255371 0.987500011920929, 0.21312499046325684 0.96875, 0.20166671276092529 0.956250011920929, 0.19249999523162842 0.93125000596046448, 0.18104168772697449 0.91249999403953552, 0.171875 0.88124999403953552, 0.16270831227302551 0.84999999403953552, 0.15125000476837158 0.8125, 0.14208328723907471 0.77500000596046448, 0.13062500953674316 0.737500011920929, 0.12145829200744629 0.6875, 0.11000001430511475 0.643750011920929, 0.10083329677581787 0.59375, 0.091666668653488159 0.543749988079071, 0.080208331346511841 0.48750001192092896, 0.071041673421859741 0.43125000596046448, 0.059583336114883423 0.36875000596046448, 0.050416678190231323 0.3125, 0.041249990463256836 0.25, 0.029791682958602905 0.1875, 0.020624995231628418 0.125, 0.0091666579246521 0.0625, 0 0)}
 
 DECLARE @g geometry;
DECLARE @h geometry;
 select @g = geometry::STGeomFromText('LINESTRING (0.99916769726371513 -1, 0.99916769726371513 1)', 0)
 select @h = geometry::STGeomFromText('LINESTRING (0.99916669726371765 0, 0.99000009894371033 -0.0625, 0.98083341121673584 -0.125, 0.96937510371208191 -0.1875, 0.960208386182785 -0.25, 0.94875010848045349 -0.3125, 0.93958339095115662 -0.36875000596046448, 0.93041670322418213 -0.43125000596046448, 0.9189583957195282 -0.48750001192092896, 0.90979170799255371 -0.543749988079071, 0.89833340048789978 -0.59375, 0.88916671276092529 -0.643750011920929, 0.87770840525627136 -0.6875, 0.86854168772697449 -0.737500011920929, 0.85937508940696716 -0.77500000596046448, 0.84791669249534607 -0.8125, 0.83875009417533875 -0.84999999403953552, 0.82729169726371765 -0.88124999403953552, 0.81812509894371033 -0.91249999403953552, 0.80895841121673584 -0.93125000596046448, 0.79750010371208191 -0.956250011920929, 0.788333386182785 -0.96875, 0.77687498927116394 -0.987500011920929, 0.76770839095115662 -0.99375000596046448, 0.75854170322418213 -1, 0.7470833957195282 -1, 0.73791670799255371 -0.99375000596046448, 0.72645840048789978 -0.987500011920929, 0.71729171276092529 -0.981249988079071, 0.708125114440918 -0.96250000596046448, 0.69666668772697449 -0.94374999403953552, 0.68750008940696716 -0.925000011920929, 0.67604169249534607 -0.893750011920929, 0.66687509417533875 -0.86875000596046448, 0.65770828723907471 -0.831250011920929, 0.64625009894371033 -0.793749988079071, 0.63708341121673584 -0.75624999403953552, 0.62562501430511475 -0.71250000596046448, 0.616458386182785 -0.668749988079071, 0.60499998927116394 -0.61875000596046448, 0.59583339095115662 -0.56874999403953552, 0.58666670322418213 -0.512499988079071, 0.5752083957195282 -0.45625001192092896, 0.56604170799255371 -0.40000000596046448, 0.55458340048789978 -0.34375, 0.54541671276092529 -0.28125, 0.536250114440918 -0.21875, 0.52479168772697449 -0.15625, 0.51562508940696716 -0.09375, 0.50416669249534607 -0.03125, 0.49500000476837158 0.03125, 0.48583340644836426 0.09375, 0.47437500953674316 0.15625, 0.46520841121673584 0.21875, 0.45375001430511475 0.28125, 0.44458338618278503 0.34375, 0.43541669845581055 0.40000000596046448, 0.42395839095115662 0.45625001192092896, 0.41479170322418213 0.512499988079071, 0.4033333957195282 0.56874999403953552, 0.39416670799255371 0.61875000596046448, 0.38270840048789978 0.668749988079071, 0.37354171276092529 0.71250000596046448, 0.36437499523162842 0.75624999403953552, 0.35291668772697449 0.793749988079071, 0.34375 0.831250011920929, 0.33229169249534607 0.86875000596046448, 0.32312500476837158 0.893750011920929, 0.31395828723907471 0.925000011920929, 0.30250000953674316 0.94374999403953552, 0.29333341121673584 0.96250000596046448, 0.28187501430511475 0.981249988079071, 0.27270838618278503 0.987500011920929, 0.26354169845581055 0.99375000596046448, 0.25208339095115662 1, 0.24291670322418213 1, 0.2314583957195282 0.99375000596046448, 0.22229170799255371 0.987500011920929, 0.21312499046325684 0.96875, 0.20166671276092529 0.956250011920929, 0.19249999523162842 0.93125000596046448, 0.18104168772697449 0.91249999403953552, 0.171875 0.88124999403953552, 0.16270831227302551 0.84999999403953552, 0.15125000476837158 0.8125, 0.14208328723907471 0.77500000596046448, 0.13062500953674316 0.737500011920929, 0.12145829200744629 0.6875, 0.11000001430511475 0.643750011920929, 0.10083329677581787 0.59375, 0.091666668653488159 0.543749988079071, 0.080208331346511841 0.48750001192092896, 0.071041673421859741 0.43125000596046448, 0.059583336114883423 0.36875000596046448, 0.050416678190231323 0.3125, 0.041249990463256836 0.25, 0.029791682958602905 0.1875, 0.020624995231628418 0.125, 0.0091666579246521 0.0625, 0 0)', 0)
 select @h.STIntersection(@g).ToString();
 
select geometry::STGeomFromText('POLYGON ((0 -1, 0.99916669726371765 -1, 0.99916669726371765 1, 0 1, 0 -1)', 0)